﻿using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace VSAdditionalCommands.Commands
{
    [Command(PackageIds.CopyMethodCommand)]
    internal sealed class CopyMethodCommand : BaseCommand<CopyMethodCommand>
    {
        /// <summary>
        /// Command: скопировать метод на котором каретка в буфер
        /// </summary>
        protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
        {
            await Package.JoinableTaskFactory.SwitchToMainThreadAsync();
            DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync();
            if (docView?.TextView == null) return;
            SnapshotPoint position = docView.TextView.Caret.Position.BufferPosition;

            if (docView.TextBuffer != null)
            {
                var snapshot = docView.TextBuffer.CurrentSnapshot;
                var text = snapshot.GetText(new Span(0, position.Position + 1));
                var methodBeginPositionRegex = new Regex(@"\s*(private|public|protected)", RegexOptions.RightToLeft);
                var methodBeginPosition = methodBeginPositionRegex.Match(text);
                if (methodBeginPosition.Success)
                {
                    var startMethodToEndText = snapshot.GetText(new Span(methodBeginPosition.Index, snapshot.Length - methodBeginPosition.Index));

                    int curlyBracesOpenedCount = -1;
                    int pos = 0;
                    var charr = startMethodToEndText.ToCharArray();
                    do
                    {
                        char ch = charr[pos++];
                        if (ch == '{')
                        {
                            if (curlyBracesOpenedCount == -1)
                            {
                                curlyBracesOpenedCount = 1;
                            }
                            else
                            {
                                curlyBracesOpenedCount++;
                            }
                        }
                        else if (ch == '}')
                        {
                            if(curlyBracesOpenedCount > 0)
                            {
                                curlyBracesOpenedCount--;
                            }
                            else
                            {
                                Debug.WriteLine($"WRONG curlyBracesOpenedCount {curlyBracesOpenedCount}");
                                return;
                            }
                        }
                    } 
                    while ((curlyBracesOpenedCount == -1 
                            || curlyBracesOpenedCount > 0) 
                                && pos < charr.Length);
                    Clipboard.SetText(startMethodToEndText.Substring(0, pos));
                }

            }
        }
    }
}
